import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/widget_extension.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/core/utils/log_util.dart';

extension AssetsX on String {
  Widget asImg({
    /// 点击图片是否显示大图
    bool clickShowLarge = false,

    /// 图片加载失败时的处理
    Widget Function()? handleErr,
    double? height,
    double? width,

    /// 图片的适应模式，仅对非svg起作用
    BoxFit? fit,

    /// 圆角
    double? radius,
  }) {
    var img = () {
      if (endsWith('.svg')) return SvgPicture.asset(this, height: height, width: width);
      if (endsWith('.png') || endsWith('.jpg') || endsWith('.jpeg') || endsWith('.gif') || endsWith('.webp')) {
        if (startsWith('http')) {
          if(Interface.fileUrl != 'http://192.168.50.67:9090') {
            // 外网
            if(startsWith('http://172.13.200.20:9090')) {
              // 把开头 http://172.13.200.20 ，则替换为 http://219.145.189.219
              return Image.network(replaceFirst('http://172.13.200.20:9090', 'http://219.145.189.219:9090'), height: height, width: width, fit: fit, errorBuilder: (BuildContext context, Object error, StackTrace? stackTrace) {
                return handleErr?.call() ?? const Icon(Icons.error);
              });
            } else if(startsWith('http://172.13.200.20:9089')) {
              return Image.network(replaceFirst('http://172.13.200.20:9089', 'http://219.145.189.219:9090'), height: height, width: width, fit: fit, errorBuilder: (BuildContext context, Object error, StackTrace? stackTrace) {
                return handleErr?.call() ?? const Icon(Icons.error);
              });
            }
            else if(startsWith('http://192.168.50.67:9090')) {
              return Image.network(replaceFirst('http://192.168.50.67:9090', 'http://219.145.189.219:9090'), height: height, width: width, fit: fit, errorBuilder: (BuildContext context, Object error, StackTrace? stackTrace) {
                return handleErr?.call() ?? const Icon(Icons.error);
              });
            } else {
              return Image.network(this, height: height, width: width, fit: fit, errorBuilder: (BuildContext context, Object error, StackTrace? stackTrace) {
                return handleErr?.call() ?? const Icon(Icons.error);
              });
            }
          }
          // 内网
          return Image.network(this, height: height, width: width, fit: fit, errorBuilder: (BuildContext context, Object error, StackTrace? stackTrace) {
            return const Icon(Icons.error);
          });
        } else {
          return Image.network(Interface.fileUrl + this, height: height, width: width, fit: fit, errorBuilder: (BuildContext context, Object error, StackTrace? stackTrace) {
            AppLogger.log('图片加载失败：$this');
            return handleErr?.call() ?? Container();
          });
        }
      }
    }();
    if (radius != null && img != null) img = ClipRRect(borderRadius: BorderRadius.circular(radius), child: img);
    if (img != null) return clickShowLarge ? img.clickShowLarge(url: this) : img;

    return AppGaps.zero;
  }
}
